<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/5/30
 * Time: 22:57
 * Mail:87658989@qq.com
 */

namespace app\mobile\controller;
use EasyWeChat\Factory;
use think\Controller;
class Base extends Controller
{
    public $app;
    public function _initialize(){
        $config = [
            'app_id'        => 'wxfebffdb82a23a32a',
            'secret'        => '42cc526ba852b9d64207e545c2891256',
            'response_type' => 'array',
            'token'=>'aierfentoken',
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/mobile/wechat/callBack',
            ],
            'log'  => [
                'level' => 'debug',
                'file' => LOG_PATH.'wechat.log',
            ],
        ];
        $this->app = Factory::officialAccount($config);
        $config = $this->app->jssdk->buildConfig(array('onMenuShareAppMessage', 'onMenuShareTimeline'));//,true 加true调试
        $this->assign('config',$config);
    }

    public function callBack(){
        $oauth = $this->app->oauth;
        // 获取 OAuth 授权结果用户信息
        $user = $oauth->user();

        $_SESSION['wechat_user'] = $user->toArray();

        $targetUrl = empty($_SESSION['target_url']) ? '/' : $_SESSION['target_url'];

        header('location:'. $targetUrl); // 跳转到 user/profile
    }

    public function retSucc($msg,$arr=[]){
        $data['code'] = 1;
        $data['msg']  = 'ok';
        $data['data'] = $msg;
        array_merge($data,$arr);
        return json($data,200);
    }

    public function retErr($msg){
        $data['code'] = 0;
        $data['msg']  = $msg;
        return json($data,200);
    }

    public function test()
    {
        $openid      = input('openid');
        $accessToken = $this->app->access_token;
        $token       = $accessToken->getToken();
        $url         = 'https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=' . $token['access_token'];
        $data = [
            'touser' => $openid,
            'template_id' => '802xVIZzAnXhPqb25Dt8ppqYDpPvl6LxrIy8VP997gY',
            'url' => 'http://www.aierfen.com/mobile/wechat/test2',
            'scene' => 1000,
            'data' => [
                'content' => [
                    "value" => '报名成功'
                ]
            ],
        ];
        $data_string = json_encode($data);
        $this->vpost($url, $data_string);
    }

    public function vpost($url, $data)
    { // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
}
